<template>
  <div class="approval-base">
    <div class="con">
      <div class="opts-button">
        <el-button
          size="medium"
          v-if="user.Id== affairs.Creator.Id && affairs.Cancelable"
          :type="isText?'text':'primary'"
        >
          撤销
        </el-button>
        <el-button
          size="medium"
          v-if="user.Id== affairs.Creator.Id && affairs.State =='Reject'"
          :type="isText?'text':'primary'"
          @click="edit($event)"
        >
          编辑
        </el-button>
        <el-button
          size="medium"
          v-else-if="isMe && affairs.Next && affairs.Next.length>0"
          :type="isText?'text':'primary'"
        >{{ exe.ItemName?exe.ItemName:'处理' }}</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import cell from '@/components/form/formcell.vue'
import { approvalOpsUrl } from '@/api/approval/index'
export default {
  name: 'HomeIcons',
  components: {
    cell,
  },
  props: {
    'affair': {
      type: Object,
      required: true
    },
    'tps': {
      type: Object,
      default: {}
    },
    'isText': {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      isVisible: false,
      template: {
        Remark: '',
        Files: []
      },
      type: '',
      StepId: '',
      isMe: false,
      user: {},
      btnMaps: {},
      exe: null,
      affairs: {}
    }
  },
  computed: {
  },
  watch: {
    affair: {
      handler(newVal) {
        this.affairs = newVal
        this.init()
      },
      deep: true
    }
  },
  created() {
    this.affairs = this.affair
    this.user = {Id:this.$store.getters.userInfo.user.id}
    this.init()
  },
  methods: {
    edit(event) {
      this.$emit('toEdit', this.affairs)
      event.cancelBubble = true
    },
    init() {
      if (!this.affairs) {
        return
      }
      this.isMe = false
      this.btnMaps = {}
      if (this.affairs.Next) {
        for (const exe of this.affairs.Next) {
          if (exe) {
            for (const s of exe.Staffs) {
              if (s == this.user.Id) {
                this.isMe = true
                this.StepId = exe.StepId
                this.exe = exe
                for (const btn of exe.Buttons) {
                  this.$set(this.btnMaps, btn.Type, btn)
                }
                return
              }
            }
          }
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
